package com.zb.service.imp.sys;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zb.dao.base.BaseDao;
import com.zb.dao.ext.charger.OmsChargerDao;
import com.zb.dao.ext.station.OmsStationDao;
import com.zb.dao.ext.system.OmsOperListDao;
import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.charger.OmsCharger;
import com.zb.entity.station.OmsStation;
import com.zb.entity.station.OmsStationLocation;
import com.zb.entity.system.OmsOperList;
import com.zb.service.imp.base.BaseService;
import com.zb.service.sys.EnterpriseService;
import com.zb.service.sys.StationService;
import com.zb.tcp.rpc.ms.SystemMasterServerRpcClient;
import com.zb.tcp.rpc.ms.client.entity.MasterQueryCardsVO.MasterQueryCardsParamVO;
import com.zb.tcp.rpc.ms.client.entity.MasterQueryCardsVO.MasterQueryCardsRetVO;
import com.zb.util.StringUtil;
import com.zb.util.serialGenerater;
import com.zb.util.OmsConstant.MsgCodeEnum;
import com.zb.vo.oms.OmsStationVO;

import tt.dz.base.authentication.entity.LoginParam;
import tt.dz.base.authentication.entity.RPCLoginRes;
import tt.dz.base.authentication.service.AuthenService;
import tt.dz.base.common.entity.ApiResult;
/**
 * enterpriselogins 企业级客户
 * @author Administrator
 *
 */
@Service(value="enterpriseService") @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class EnterpriseServiceLogin  extends BaseService implements EnterpriseService{
	private static Logger log = LoggerFactory.getLogger(EnterpriseServiceLogin.class);
//	@Autowired
//	private SystemMasterServerRpcClient systemMasterServerRpcClient;
	@Autowired
	private OmsOperListDao operatorDaoImpl;
	@Autowired
	private OmsStationDao omsStationDao;
	@Autowired
	public StationService stationService;
	@Autowired
	private OmsChargerDao omsChargerDao;
	
	@Autowired
	@Qualifier("base")
	private BaseDao<OmsStationLocation> baseDao;
	@Autowired
	@Qualifier("base")
	private BaseDao<OmsOperList> baseOperListDao;
	

	//authenService
	@SuppressWarnings("rawtypes")
	public JsonCommon<?> thirdPartLogin(String usename, String password) {
		
			return buildError400JsonCommon();
		
		
	}

		//查询站点信息
		@SuppressWarnings("rawtypes")
		@Override
		public JsonForList getStationListByPage(OmsStationVO station, int pageNo, int pageSize, String sortName,
				int sortVal, String oper_id) {
				return omsStationDao.getStationListByPage(station.getStation(), pageNo, pageSize, sortName, sortVal, oper_id);
		}
		/**
		 * 
		 */
		@SuppressWarnings({ "rawtypes", "unchecked" })
		@Override
		public JsonForList<?> getStationByPlantId(String station_id) {
			String sql = "SELECT b.*,c.*,d.* from oms_station a LEFT JOIN oms_charger b on a.station_id=b.station_id LEFT JOIN "+
					"oms_charger_state_alternate c on b.charger_id=c.charger_id LEFT JOIN "
					+ "oms_charger_state_direct d on b.charger_id=d.charger_id where a.station_id='"+station_id+"'"
					+ " and b.valid_flag=1 and b.is_allow_open=1";
					
		List<Map<String,Object>> retListMap = this.omsChargerDao.findDataBySql(sql, null);
		JsonForList json=new JsonForList<>();
		if(retListMap.size()!=0){
			json.setList(retListMap);
			json.setCode(MsgCodeEnum.SUCCESS.getMsgCode());
			json.setMsg(MsgCodeEnum.SUCCESS.getMsgInfo());
			
		}else{
			json.setCode(MsgCodeEnum.ERROR400.getMsgCode());
			json.setMsg(MsgCodeEnum.ERROR400.getMsgInfo());
		}
			return json;
		}
		@SuppressWarnings("rawtypes")
		@Override
		public JsonForList getStationList(OmsStation station, Integer pageNo,Integer pageSize,String sortName,Integer sortVal,String groupByStrs) {
			JsonForList<OmsStation> retJsons = new JsonForList<OmsStation>();
			List<OmsStation> list=omsStationDao.findListByCondition(station, "where valid_flag=1");
			if(list.size()<pageSize){
				pageNo=0;
				pageSize=list.size();
			}
			retJsons.setList(omsStationDao.findListByPageAndGroupAndOrderAndCondition(station, "where valid_flag=1",pageNo,pageSize,sortName,sortVal,groupByStrs));
			retJsons.setCode(MsgCodeEnum.SUCCESS.getMsgCode());
			retJsons.setMsg(MsgCodeEnum.SUCCESS.getMsgInfo());
			return retJsons;
		}
		@Override
		public List<OmsStationLocation> findByStationId(String id, Integer pageNo, Integer pageSize, String sortName,
				Integer sortVal) {
			try 
			{
				OmsStationLocation queryLocation = new OmsStationLocation();
				queryLocation.setStation_id(id);
				List<OmsStationLocation> lsStationLoc = baseDao.findExample(queryLocation, pageNo, pageSize, sortName, 1);
				return lsStationLoc;
			} 
			catch (Exception e) 
			{
				this.logError(e.getMessage(), e);
				return null;
			}
		}
		@SuppressWarnings({ "rawtypes", "unchecked" })
		@Override
		public JsonForList getCharsByStationId(String station_id) {
			OmsCharger station=new OmsCharger();
			JsonForList<OmsCharger> retJsons = new JsonForList<>();
			List list=omsChargerDao.findListByCondition(station, "where station_id='"+station_id+"' and valid_flag=1");
			if(list.size()!=0){
				retJsons.setList(list);
				retJsons.setCode(MsgCodeEnum.SUCCESS.getMsgCode());
				retJsons.setMsg(MsgCodeEnum.SUCCESS.getMsgInfo());
			}else{
				retJsons.setCode(MsgCodeEnum.ERROR400.getMsgCode());
				retJsons.setMsg(MsgCodeEnum.ERROR400.getMsgInfo());
			}
			return retJsons;
		}
		
		
}
